<template>
  <div class="box">
    <h2>首页 / 文章列表</h2>
    <el-table :data="postList" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column label="发布时间" width="170">
        <template slot-scope="scope">
          <span style="margin-left: 10px">
            {{ scope.row.create_date.split("T")[0] }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="缩略图" width="280">
        <template slot-scope="scope">
          <!-- scope.row.cover.length > 0  是判断如果有这个数组就渲染图片 没有就显示本地图片 -->
          <img
            class="thumbnail"
            v-if="scope.row.cover.length > 0"
            :src="fixUrl(scope.row.cover[0].url)"
            alt=""
          />
          <img
            class="thumbnail"
            v-else
            src="../images/fa02c509aa9e05cac9bba6ee98fa9d6a.png"
            alt=""
          />
        </template>
      </el-table-column>

      <el-table-column
        prop="title"
        label="文章标题"
        width="300"
      ></el-table-column>
      <el-table-column
        prop="user.nickname"
        label="作者"
        width="240"
      ></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="$router.push('/editpost?id=' + scope.row.id)"
            ><i class="el-icon-edit"></i
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total,prev, pager, next,jumper,sizes"
      :total="total"
      :page-size="pageSize"
      :page-sizes="[2, 5, 10]"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0,
      postList: [],
      pageIndex: 1,
      pageSize: 3,
    };
  },
  created() {
    this.loadPost();
  },
  methods: {
    fixUrl(oldUrl) {
      const regExp = /^http/;
      if (regExp.test(oldUrl)) {
        return oldUrl;
      } else {
        return this.$axios.defaults.baseURL + oldUrl;
      }
    },
    loadPost() {
      this.$axios({
        url: "post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        console.log(res.data);
        this.postList = res.data.data;
        this.total = res.data.total;
      });
    },
    //点击分页组件的时候可以触发,当前页码的变更
    currentChange(newIndex) {
      //修改当前页码为点击的数字
      this.pageIndex = newIndex;
      // 重新加载数据即可
      this.loadPost();
    },
    sizeChange(newSize) {
      //修改每页的长度
      this.pageSize = newSize;
      //刷新页面
      this.loadPost();
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  h2 {
    margin-top: -50px;
    padding-bottom: 40px;
  }
  .thumbnail {
    border-radius: 30px;
    width: 180px;
    height: 120px;
    object-fit: cover;
  }
  .el-pagination {
    text-align: center;
    margin-top: 40px;
  }
  .el-button--default {
    background-color: #409eff;
    color: #fff;
    font-size: 18px;
  }
  // .el-table th > .cell {
  //   padding-left: 35px;
  // }
  // /deep/ .el-table td,
  // .el-table th {
  //   text-align: center !important;
  // }
}
</style>